#include <assert.h>

#include "GameManager.h"
#include "Dict.h"

GameManager::GameManager()
	:mDict(NULL)
{

}

GameManager::~GameManager()
{
	for(auto it = mGames.begin(); it != mGames.end(); it++)
	{
		delete(*it);
	}

	mGames.clear();
}

GameManager& GameManager::getInstance()
{
	static GameManager smInstance;

	return smInstance;
}

Game* GameManager::create(const std::string& gameCookie)
{
	auto it = mGames.begin();
	auto itend = mGames.end();

	for(; it != itend; it++)
	{
		if((*it)->gameCookie == gameCookie)
		{
			assert(0);
		}
	}

	Game* game = new Game();
	game->gameCookie = gameCookie;
	mGames.push_back(game);

	return game;
}

Game* GameManager::getGame(const std::string& gameCookie)
{
	auto it = mGames.begin();
	auto itend = mGames.end();

	for(; it != itend; it++)
	{
		if((*it)->gameCookie == gameCookie)
		{
			return *it;
		}
	}

	return NULL;
}

Game* GameManager::getCurrentGame()
{
	return getGame(mCurrentGameCookie);
}

bool GameManager::loadDict(const char* dictFileName)
{
	if(mDict)
	{
		delete mDict;
		mDict = NULL;
	}

	mDict = new Dict();

	unsigned int numOfWordLoaded = mDict->loadFile("words_en.txt");

	return numOfWordLoaded != 0;
}

bool GameManager::isWord(const std::string& word)
{
	if(!mDict)
	{
		return false;
	}

	return mDict->isWord(word.c_str());
}